package com.librarysys.service.impl;

import com.librarysys.common.Const;
import com.librarysys.common.Result;
import com.librarysys.dao.ManagerMapper;
import com.librarysys.dao.PurviewMapper;
import com.librarysys.entity.Manager;
import com.librarysys.entity.Purview;
import com.librarysys.service.IManagerService;
import com.librarysys.vo.ManagerPurviewVO;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuhui on 2017/7/11.
 */
@Service("iManagerService")
public class ManagerServiceImpl implements IManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private PurviewMapper purviewMapper;


    @Override
    public Result login(String name, String password) {
        Manager manager = managerMapper.selectByNameAndPassword(name, password);
        if(manager==null){
            return Result.createByError("用户名或密码错误");
        }else{
            return Result.createBySuccess("登陆成功",manager);
        }
    }


    /**
     * 验证用户名是否存在
     * @return
     */
    public Boolean check(String name){
        Integer integer = managerMapper.selectByName(name);
        if(integer>0){
            return true;
        }else{
            return false;
        }
    }

    public Result modify(String name,String oldPassword,String newPassword){
        Integer integer = managerMapper.updateByNameAndPassword(name, oldPassword, newPassword);
        if(integer>0){
            return Result.createBySuccess("修改密码成功");
        }else{
            return Result.createByError("修改密码失败");
        }
    }

    public Result listManager(){
        List<Manager> managers = managerMapper.selectAll();
        List<ManagerPurviewVO> list=new ArrayList<ManagerPurviewVO>();
        for(Manager m:managers){
            Purview purview = purviewMapper.selectByPrimaryKey(m.getId());
            ManagerPurviewVO managerPurviewVO = new ManagerPurviewVO(m.getName(),purview.getBookset(),purview.getBorrowback(),
                    purview.getReaderset(),purview.getSysquery(), purview.getSysset(),m.getId());
            list.add(managerPurviewVO);
        }
        return Result.createBySuccess("获取成功",list);
    }

    public Result addManager(Manager manager){
        int insert = managerMapper.insert(manager);
        if(insert>0){
            purviewMapper.insert(new Purview(manager.getId(),false,false,false,false,false));
            return Result.createBySuccess("添加成功");
        }else{
            return  Result.createByError("添加失败");
        }
    }

    public Result delManager(Integer id){
        if(id==null){
            return Result.createByError("参数错误");
        }
        Manager manager = managerMapper.selectByPrimaryKey(id);
        if(manager.getRole()== Const.Role.SUPER_ADMIN){
            return Result.createByError("超级管理员不能删除");
        }else{
            int i = managerMapper.deleteByPrimaryKey(id);
            if(i>0){
                purviewMapper.deleteByPrimaryKey(id);
                return Result.createBySuccess("删除成功");
            }else{
                return Result.createByError("删除失败");
            }
        }
    }

    public Result addPurview(Purview purview){
        Manager manager = managerMapper.selectByPrimaryKey(purview.getId());
        if(manager==null || manager.getRole()==Const.Role.SUPER_ADMIN){
            return  Result.createByError("超级管理员的权限不能修改");
        }
        int insert = purviewMapper.updateByPrimaryKey(purview);
        if(insert>0){
            return Result.createBySuccess("权限修改成功");
        }else{
            return Result.createByError("权限修改失败");
        }
    }
}
